<?php

namespace App\HttpController;

use EasySwoole\Core\Http\AbstractInterface\Controller;
use EasySwoole\Core\Swoole\Task\TaskManager;
use EasySwoole\Core\Swoole\Process\ProcessManager;
use App\Model\Runner;
use App\Model\Queue;
use App\Model\TaskBean;
use App\Utility\Db\Redis;
use think\Db;

/**
 * Class Index.
 */
class Index extends Controller
{
    /**
     * 首页方法.
     *
     * @author : evalor <master@evalor.cn>
     */
    public function index()
    {
        $this->response()->withHeader('Content-type', 'text/html;charset=utf-8');
        $this->response()->write('<div style="text-align: center;margin-top: 30px"><h2>欢迎使用EASYSWOOLE</h2></div></br>');
        $this->response()->write('<div style="text-align: center">您现在看到的页面是默认的 Index 控制器的输出的页面anbell 0.0.3</div></br>');
        $this->response()->write('<div style="text-align: center"><a href="http://39.104.65.45:9501/index/checkWorker">查看检测情况http://39.104.65.45:9501/index/checkWorker</a></div></br>');
		$this->response()->write('<div style="text-align: center"><a href="http://39.104.65.45sql.shangning.org.cn/">数据库</a></div></br>');
		$this->response()->write('<div style="text-align: center"><a href="https://love.163ar.cn/api/home/show">查看检查状态</a></div></br>');
	}
  
    private function addtask()
    {
        /**数据库循环检测 */
        TaskManager::async(function () {
            $bean = new TaskBean();
            $checkDomainList = Db::name('wxurls')->where('status', 1)->order('id desc')->select();
            foreach ($checkDomainList as $domain) {
                $bean->setUrl($domain);
                Queue::set($bean);
            }
        });
    }

    //执行这个
    public function checkWorker()
    {
        $name = ProcessManager::getInstance()->getProcessByName('custom');
        $name->addTick(2 * 1000, function () {
            TaskManager::async(Runner::class);
            if (!Redis::getInstance()->lSize('task_list')) {
                self::addtask();
            }
        });
        $this->response()->withHeader('Content-type', 'text/html;charset=utf-8');
        $this->response()->write('防封系统F0.0.3 写入本地SQL日志 启动成功,请关闭页面 http://39.104.65.45sql.shangning.org.cn/');
    }

    // ========= temp  code

    public function readLogs(){
        echo readfile('./logs/requestMsg'.date('YmdH',time()).'.log');
    }
        // 每小时写入日志记录
        public function rlog3(){
            file_put_contents('./logs/requestMsg'.date('YmdH',time()).'.log', '测试系统日志记录:'.date('Y-m-d : h:i:sa',time())."\n", FILE_APPEND);
        }
}
